<template>
  <div class="component-distribution-auditing">
    <!-- 搜索栏 -->
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        class="search-form"
        :model="searchForm"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item :label="$t('distributionMsg.nickName') + ':'">
            <el-input
              v-model="searchForm.nickName"
              type="text"
              clearable
              :placeholder="$t('distributionMsg.nickName')"
            />
          </el-form-item>
          <el-form-item :label="$t('distributionMsg.distriTelPhone') + ':'">
            <el-input
              v-model="searchForm.userMobile"
              type="text"
              clearable
              :placeholder="$t('distributionMsg.distriTelPhone')"
            />
          </el-form-item>
          <el-form-item
            prop="state"
            :label="$t('distributionMsg.auditStatus') + ':'"
          >
            <el-select
              v-model="searchForm.state"
              :placeholder="$t('distributionMsg.auditStatus')"
              clearable
            >
              <el-option
                :label="$t('product.pendingReview')"
                :value="0"
              />
              <el-option
                :label="$t('productComm.pass')"
                :value="1"
              />
              <el-option
                :label="$t('productComm.noPass')"
                :value="-1"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('crud.searchBtn') }}
            </div>
            <div
              class="default-btn"
              @click="onResetSearch('searchForm')"
            >
              {{ $t('product.reset') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>

    <div class="main-container">
      <!-- 表格 -->
      <div class="table-con distribution-auditing-table">
        <el-table
          ref="distributionAuditingTableRef"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row-low"
          style="width: 100%"
          @sort-change="sortChange"
        >
          <!-- 昵称 -->
          <el-table-column
            prop="nickName"
            :label="$t('users.nickName')"
          >
            <template #default="scope">
              {{ scope.row.nickName || $t('order.userLoggedOut') }}
            </template>
          </el-table-column>
          <!-- 分销员手机号 -->
          <el-table-column
            width="150"
            prop="userMobile"
            :label="$t('distributionMsg.distriTelPhone')"
          >
            <template #default="scope">
              {{ scope.row.userMobile || $t('chat.none') }}
            </template>
          </el-table-column>
          <!-- 邀请人手机号 -->
          <el-table-column
            width="150"
            prop="parentUserMobile"
            :label="$t('distributionMsg.inviteesPhoneNumber')"
          >
            <template #default="scope">
              <span v-if="!scope.row.parentUserMobile">无</span>
              <span v-else>{{ scope.row.parentUserMobile }}</span>
            </template>
          </el-table-column>
          <!-- 邀请人 -->
          <el-table-column
            prop="parentNickName"
            :label="$t('distributionMsg.invitees')"
          >
            <template #default="scope">
              <span v-if="!scope.row.parentNickName">无</span>
              <span v-else>{{ scope.row.parentNickName }}</span>
            </template>
          </el-table-column>
          <!-- 积累消费金额 -->
          <el-table-column
            prop="sumOfConsumption"
            width="180"
            :label="$t('distributionMsg.accumulatedConsumptionAmount')"
            sortable="custom"
          >
            <template #default="scope">
              <span v-if="!scope.row.sumOfConsumption">0</span>
              <span v-else>{{ scope.row.sumOfConsumption }}</span>
            </template>
          </el-table-column>
          <!-- 积累消费笔数 -->
          <el-table-column
            prop="expenseNumber"
            width="180"
            :label="$t('distributionMsg.accumulatedConsumptionTimes')"
            sortable="custom"
          >
            <template #default="scope">
              <span v-if="!scope.row.expenseNumber">0</span>
              <span v-else>{{ scope.row.expenseNumber }}</span>
            </template>
          </el-table-column>
          <!-- 加入时间 -->
          <el-table-column
            prop="auditingTime"
            width="180"
            :label="$t('order.applicationTime')"
            sortable="custom"
          >
            <template #default="scope">
              {{ scope.row.auditingTime }}
            </template>
          </el-table-column>
          <!-- 审核状态 -->
          <el-table-column
            prop="state"
            :label="$t('distributionMsg.auditStatus')"
          >
            <template #default="scope">
              <div
                v-if="scope.row.state === -1"
                class="tag-text"
              >
                {{ $t("productComm.noPass") }}
              </div>
              <div
                v-if="scope.row.state === 0"
                class="tag-text"
              >
                {{ $t("product.pendingReview") }}
              </div>
              <div
                v-if="scope.row.state === 1"
                class="tag-text"
              >
                {{ $t("productComm.pass") }}
              </div>
            </template>
          </el-table-column>
          <!-- 操作人 -->
          <el-table-column
            prop="sysUsername"
            :label="$t('distribUserWallet.operator')"
          >
            <template #default="scope">
              {{ scope.row.sysUsername || '-' }}
            </template>
          </el-table-column>
          <!-- 操作 -->
          <el-table-column
            fixed="right"
            align="center"
            :label="$t('publics.operating')"
            width="150"
          >
            <template #default="scope">
              <div class="text-btn-con">
                <div
                  v-if="scope.row.state === 0 && isAuth('distribution:distributionAuditing:update')"
                  class="default-btn text-btn"
                  @click="onAddOrUpdate(scope.row,true)"
                >
                  {{ $t('productComm.audit') }}
                </div>
                <div
                  v-if="scope.row.state !== 0 && isAuth('distribution:distributionAuditing:info')"
                  class="default-btn text-btn"
                  @click="onAddOrUpdate(scope.row,false)"
                >
                  {{ $t('seckill.view') }}
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 分页 -->
      <el-pagination
        v-if="dataList.length"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="onPageSizeChange"
        @current-change="onPageChange"
      />
    </div>

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdateRef"
      @refresh-data-list="refreshChange"
    />
  </div>
</template>

<script setup>
import { isAuth } from '@/utils'
import AddOrUpdate from './distribution-auditing-update.vue'

onMounted(() => {
  getDataList()
})

let sortParams = null
const sortChange = (column) => {
  const sortParam = {}
  switch (column.prop) {
    case 'sumOfConsumption': sortParam.sortParam = 1
      break
    case 'expenseNumber': sortParam.sortParam = 2
      break
    case 'auditingTime': sortParam.sortParam = 3
      break
    default: sortParam.sortParam = 0
      break
  }
  // 排序类型 0无 1 正序 2倒序
  switch (column.order) {
    case 'descending': sortParam.sortType = 2
      break
    case 'ascending': sortParam.sortType = 1
      break
    default: sortParam.sortType = 0
      break
  }
  sortParams = sortParam
  getDataList()
}

const addOrUpdateVisible = ref(false)
const addOrUpdateRef = ref(null)
/**
 * 新增 / 修改
 * @param data
 * @param isEdit
 */
const onAddOrUpdate = (data, isEdit) => {
  addOrUpdateVisible.value = true
  nextTick(() => {
    addOrUpdateRef.value?.init(data, isEdit)
  })
}
/**
 * 点击查询
 * @param newData
 */
const onSearch = (newData = false) => {
  page.currentPage = 1
  getDataList(page, newData)
}
/**
 * 刷新回调
 */
const refreshChange = () => {
  page.currentPage = 1
  getDataList(page)
}

let tempSearchForm = null // 保存上次点击查询的请求条件
const searchForm = ref({})
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const dataList = ref([])
const dataListLoading = ref(false)
/**
 * 获取数据列表
 * @param pageParam
 * @param newData
 */
const getDataList = (pageParam, newData = false) => {
  dataListLoading.value = true
  const params = Object.assign(searchForm.value, {})
  // 日期赋值
  if (params && params.dateRange) {
    params.startTime = params.dateRange[0]
    params.endTime = params.dateRange[1]
    params.dateRange = null
  }
  if (newData || !tempSearchForm) {
    tempSearchForm = JSON.parse(JSON.stringify(searchForm.value))
  }
  http({
    url: http.adornUrl('/distribution/distributionAuditing/page'),
    method: 'get',
    params: http.adornParams(Object.assign({
      current: pageParam ? pageParam.currentPage : page.currentPage,
      size: pageParam ? pageParam.pageSize : page.pageSize,
      ...sortParams
    }, tempSearchForm))
  })
    .then(({ data }) => {
      page.total = data.total
      page.pageSize = data.size
      page.currentPage = data.current
      dataList.value = data.records
      dataListLoading.value = false

      // 末尾页数据为空重新请求
      if (!dataList.value.length && page.currentPage > 1) {
        page.currentPage = 1
        getDataList()
      }
    })
}

const searchFormRef = ref(null)
/**
 * 重置表单
 */
const onResetSearch = () => {
  searchFormRef.value?.resetFields()
  searchForm.value = {}
}

const onPageSizeChange = (val) => {
  page.pageSize = val
  getDataList(page)
}
const onPageChange = (val) => {
  page.currentPage = val
  getDataList(page)
}

</script>
